package com.springboot.tr.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.concurrent.ScheduledFuture;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/8/8 15:42
 */
@Component
public class CollectPerfomanceDataScheduledTask {
    @Autowired
    private MailService mailService;
    @Value("${scheduleTask.cron1}")
    private String cron1;
    @Value("${scheduleTask.cron2}")
    private String cron2;

    @Value("${collectPerfomanceDataTask.enable}")
    private boolean cpdFlag;

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    private ScheduledFuture<?> taskfuture;

    public void init() {
        System.out.println("======= collect performance init======");

        if(cpdFlag){
            taskfuture = threadPoolTaskScheduler.schedule(new CollectPerfomanceDataTask(), new CronTrigger(cron1));
        }else{
            System.out.println("性能数据采集的定时任务还未开启");
        }
    }

    //关闭定时任务
    public void stopCron(String taskKey) {
        if (taskfuture != null) {
            taskfuture.cancel(true);
        }
        // 查看任务是否在正常执行之前结束,正常true
        boolean cancelled = taskfuture.isCancelled();
        while (!cancelled) {
            taskfuture.cancel(true);
        }
    }

}
